/**
 * 初始化 vue-easytable 的封装
 *
 * @author liangbin
 */
(function(){
	var ESTable = function (elId, url, tbColumns) {
		var vm= new Vue({
			el:"#"+elId,
			data() {
				return {
					isLoading:true,
					url:url,
					pageIndex:1,
	                pageSize:10,
	                total:0,
	                queryParam:{},
					tableData: [],
					columns:  tbColumns
				}
			},
			methods:{
			    loadData(){
			    	var _this = this
			    	this.isLoading = true;
			    	$.ajax({
			    		url:this.url,
			    		data:{"pageIndex":_this.pageIndex,"pageSize":_this.pageSize,"queryParam":JSON.stringify(_this.queryParam)},
			    		type:"GET",
			    		success:function(mydata){
			    			_this.isLoading = false;
			    			_this.total=Number(mydata.total);
			    			_this.tableData=mydata.list;
			    		},
			    		error:function(){
			    			_this.isLoading = false;
			    		}
			    	})
				},
				pageChange(pageIndex){
	                this.pageIndex = pageIndex;
	                this.loadData();
	            },
	            pageSizeChange(pageSize){
	               this.pageIndex = 1;
	               this.pageSize = pageSize;
	               this.loadData();
	            },
	            setQueryParam(key,value){
	            	this.queryParam[key]=value;
	            }
			},
			created(){
				this.loadData();
				var _this=this;
				document.onkeydown = function(e){
					if(e && e.keyCode==13){ // enter 键
						_this.loadData();
		            }
			    }
			}
			
		})
		return vm;
	};
	window.ESTable = ESTable;
}());
